﻿using AnesSystem.Models;
using AnesSystem.Util;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AnesSystem.ViewModels
{
    class DoctorCountCtrlViewModel : BaseCountCtrlViewModel<DoctorCountCtrlViewModel>
    {
        #region Variables
        #endregion

        #region Variables
        private int _bisCount = 0;
        private Visibility _deleteIsVis;
        #endregion

        #region Constructor
        public DoctorCountCtrlViewModel()
        {
            if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_SC_SCDC))
                DeleteIsVis = Visibility.Visible;
            else
                DeleteIsVis = Visibility.Collapsed;
        }



        #endregion

        #region Properties
        public int BisCount
        {
            get
            {
                return _bisCount;
            }

            set
            {
                _bisCount = value;
                RaisePropertyChanged("BisCount");
            }
        }
        public Visibility DeleteIsVis
        {
            get
            {
                return _deleteIsVis;
            }

            set
            {
                _deleteIsVis = value;
                RaisePropertyChanged("DeleteIsVis");
            }
        }

        public override void UpdateBaseCountModelList()
        {
            BaseCountModelList.Clear();
            List<string> nameList = new List<string>();
            foreach (PatientLinkModel patientLinkModel in PatientLinkModelList)
            {
                nameList.Clear();
                bool onOff = true;
                string operName = "";

                //获取医生姓名
                if (string.IsNullOrEmpty(patientLinkModel.AnesthesiaRecord.OperDoctor1))
                {
                    foreach (BaseCountModel baseCountModel in BaseCountModelList)
                    {
                        if (baseCountModel.Name.Equals(""))
                        {
                            onOff = false;
                            baseCountModel.Count++;
                        }
                    }
                    if (onOff)
                    {
                        BaseCountModel baseCountModel = new BaseCountModel();
                        baseCountModel.Name = "";
                        baseCountModel.Count = 1;
                        BaseCountModelList.Add(baseCountModel);
                    }
                }
                else
                {
                    operName = DataManager.Instance.GetAccountNameByID(patientLinkModel.AnesthesiaRecord.OperDoctor1);

                    nameList.Add(operName);

                    foreach (var item in nameList)
                    {
                        if (string.IsNullOrEmpty(item)) continue;
                        foreach (BaseCountModel baseCountModel in BaseCountModelList)
                        {
                            if (baseCountModel.Name.Equals(item))
                            {
                                onOff = false;
                                baseCountModel.Count++;
                            }
                        }
                        if (onOff && !string.IsNullOrEmpty(item))
                        {
                            BaseCountModel baseCountModel = new BaseCountModel();
                            baseCountModel.Name = item;
                            baseCountModel.Count = 1;
                            BaseCountModelList.Add(baseCountModel);
                        }
                        onOff = true;
                    }
                }
            }
        }


        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods
        #endregion

        #region Public/Protected Methods
        public void Excel()
        {

            if (PatientLinkModelList == null || PatientLinkModelList.Count == 0)
            {
                MessageBoxCommon.ShowDialog("请先查询病人!", MessageBoxCommon.OK);
                return;
            }
            if (DisPatientLinkModelList == null || DisPatientLinkModelList.Count == 0)
            {
                MessageBoxCommon.ShowDialog("请先选择病人集合!", MessageBoxCommon.OK);
                return;
            }
            ExportToExcel excel = new ExportToExcel();
            excel.ExportParameterCommon("历史手术", DisPatientLinkModelList.ToList());
        }

        public void UpdateDisPatientLinkModelList(BaseCountModel SelBaseCountModel)
        {
            if (SelBaseCountModel == null) return;
            if (SelBaseCountModel.Name != null && "".Equals(SelBaseCountModel.Name))
                DisPatientLinkModelList = PatientLinkModelList.Where(O =>
                {
                    if (string.IsNullOrEmpty(O.AnesthesiaRecord.OperDoctor1)
                   )
                        return true;
                    return false;

                }).ToList();

            else if (SelBaseCountModel.Name != null)
            {
                DisPatientLinkModelList.Clear();
                //string _id = DataManager.Instance.AccountList.ToList().Find(O =>
                //{
                //    if (O.TrueName != null)
                //        return O.TrueName.Equals(SelBaseCountModel.Name);
                //    else
                //        return false;
                //}).Id.ToString();
                DisPatientLinkModelList = PatientLinkModelList.Where(O =>
                DataManager.Instance.GetAccountNameByID(O.AnesthesiaRecord.OperDoctor1).Equals(SelBaseCountModel.Name)

                ).ToList();
            }

        }

        #endregion

        #region EventHandlers

        #endregion


    }
}
